package com.tobeing.picture.picker.adapters;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.tobeing.picture.picker.R;
import com.tobeing.picture.picker.models.Folder;

import java.io.File;
import java.util.List;

/**
 * Created by Administrator on 2017/11/16/016.
 */

public class FolderAdapter extends BaseAdapter {
    private Context mContext;
    private List<Folder> mFolders;

    public FolderAdapter(Context context, List<Folder> folders){
        mContext = context;
        mFolders = folders;
    }

    @Override
    public int getCount() {
        return mFolders == null ? 0 : mFolders.size();
    }

    @Override
    public Folder getItem(int position) {
        return mFolders.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Folder folder = getItem(position);
        ViewHolder holder;
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.folder_item, null, false);
            holder = new ViewHolder();
            holder.tvFolderName = convertView.findViewById(R.id.tv_folder_name);
            holder.tvImageCount = convertView.findViewById(R.id.tv_image_count);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        if(TextUtils.isEmpty(folder.getDir())){
            holder.tvFolderName.setText("全部图片");
        }else {
            File file = new File(folder.getDir());
            holder.tvFolderName.setText(file.getName());
        }
        holder.tvImageCount.setText(folder.getImages().size() + "张");
        return convertView;
    }

    class ViewHolder{
        public TextView tvFolderName;
        public TextView tvImageCount;
    }
}
